<?php defined('DML') or die("Access locked");

class EditUsers extends MySQL{
   private $action = "";
   private $arraydata  = array();
   
   public $res;
   
   public function __construct($action, $data){
      $this->action = $action;
      $this->arraydata = $data;
      switch($this->action){
         case 'add':
            $this->AddUser();
            break;
         case 'delete':
            $this->DeleteUser();
            break;
         case 'edit':
            $this->EditUser();
            break;
      }
   }
   
   private function AddUser(){
      if($this->arraydata['login'] != "" && $this->arraydata['passw'] != "" && $this->arraydata['confrimpassw'] != "" && $this->arraydata['email'] != ""){
         if($this->CheckUserLogin($this->arraydata['login']) !== FALSE){
            $login = $this->arraydata['login'];
         }else{
            unset($this->arraydata['login']);
            $this->res['error_login'] = $add_user_error_login;
         }
         if($this->arraydata['passw'] == $this->arraydata['confrimpassw']){
            $passw = md5(trim($this->arraydata['passw']));
         }else{
            unset($this->arraydata['passw']);
            $this->res['error_passw'] = $add_user_error_passw;
         }
         require_once PATH.DS.'includes'.DS.'functions'.DS.'usersReg.php';
         if(checkMail($this->arraydata['email']) == FALSE){
            $email = $this->arraydata['email'];
            if($this->CheckUserEmail($email) == FALSE){
               unset($email);
               $this->res['error_email'] = $add_user_error_email_validation;
            }
         }else{
            unset($this->arraydata['email']);
            $this->res['error_email'] = $add_user_error_check_mail;
         }
         if(isset($login) && isset($passw) && isset($email)){
            $result = self::AddUserSQL($login, $passw, $email, $this->arraydata['role']);
            if($result == TRUE){
               $this->res['result'] = $add_user_result_true; 
            }
         }
      }else{
         $this->res['result'] = $add_user_result_false;
         unset($this->arraydata);
      }
   }
   
   private function DeleteUser(){
      
   }
   
   private function EditUser(){
      
   }
   
   private function CheckUserEmail($mail){
      $login = self::Sanitize($mail);
      $bln = TRUE;
      $this->strSQL = "SELECT `email`
                       FROM `dml_users`
                       WHERE `email`='$mail'";
      $this->Query();
      if(mysql_num_rows($this->result) !== 0){
         $bln = FALSE;
      }
      return $bln;
   }
   
   private function CheckUserLogin($login){
   	$login = self::Sanitize($login);
   	$bln = TRUE;
   	$this->strSQL = "SELECT `login`
                       FROM `dml_users`
                       WHERE `login`='$login'";
   	$this->Query();
   	if(mysql_num_rows($this->result) !== 0){
   	   $bln = FALSE;
   	}
   	return $bln;
   }
}
?>
